package main

import (
	"fmt"
	"shop-backend/internal/config"
	ordercontrollers "shop-backend/internal/order/ordercontrollers"
	usercontrollers "shop-backend/internal/user/usercontrollers"
	models "shop-backend/internal/user/usermodels"

	productmodels "shop-backend/internal/product/productmodels"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
)

func main() {
	db := config.InitDB()
	sqlDB, _ := db.DB()
	defer sqlDB.Close()

	db.AutoMigrate(&models.User{})

	db.AutoMigrate(&models.User{}, &productmodels.Product{})

	r := gin.Default()

	r.POST("/orders", ordercontrollers.CreateOrder)

	// ✅ 添加 CORS 支持
	r.Use(cors.New(cors.Config{
		AllowOrigins:     []string{"http://localhost:5173"},
		AllowMethods:     []string{"POST", "GET", "OPTIONS"},
		AllowHeaders:     []string{"Origin", "Content-Type", "Accept"},
		AllowCredentials: true,
	}))

	r.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{"message": "pong"})
	})

	r.POST("/register", usercontrollers.Register)

	err := r.Run(":8080")
	if err != nil {
		fmt.Println("服务启动失败：", err)
	}
}
